package cn.iocoder.yudao.module.trade.controller.app.fruitcard;

import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.trade.controller.app.fruitcard.vo.*;
import cn.iocoder.yudao.module.trade.service.fruitcard.FruitCardUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 水果卡")
@RestController
@RequestMapping("/fruit/card")
@RequiredArgsConstructor
@Validated
@Slf4j
public class AppFruitCardController {

    @Resource
    private FruitCardUserService fruitCardUserService;


    @PostMapping("/bind")
    @Operation(summary = "绑定卡片")
    @PreAuthenticated
    public CommonResult<AppFruitCardBindRespVO> bind(@Valid @RequestBody AppFruitCardBindReqVO vo) {

        return success(fruitCardUserService.bind(vo,getLoginUserId()));
    }

    @GetMapping("/cards-page")
    @Operation(summary = "分页当前用户所有卡片")
    @PreAuthenticated
    public CommonResult<PageResult<AppFruitCardRespVO>> cardsPage(
           @Valid  AppFruitCardPageReqVO pageReqVO) {
        PageResult<AppFruitCardRespVO> pageResult = fruitCardUserService.cardsPage(pageReqVO, getLoginUserId());
        return success(pageResult);
    }



    @PostMapping("/exchange")
    @Operation(summary = "兑换水果")
    @PreAuthenticated
    public CommonResult<AppFruitCardExchangeRespVO> exchange(@Valid @RequestBody AppFruitCardExchangeReqVO vo) {
        return success(fruitCardUserService.exchange(vo,getLoginUserId(),getClientIP()));
    }



}
